<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\pay\addons;

class Alipay
{
    /**
     * 配置   
     */
    public static function config()
    {
        return include dirname(__FILE__).'/alipay/config.php';
    }
    
    /**
     * 二维码支付请求
     * @param  string $orderId    订单号
     * @param  string $title      订单标题
     * @param  float  $rmb        订单总金额
     * @param  string $notify_url 异步通知地址
     * @param  string $body       订单描述
     * @return array             
     */
    public static function qrCodePay($orderId, $title, $rmb, $notify_url, $body = "") 
    {
        $config = self::config();
        if (empty($config)) {
            return ['status' => 'error', 'message' => '未配置支付宝支付功能'];
        }
        if ($config['open'] == 1) {
            require_once dirname ( __FILE__ ).'/alipay/f2fpay/model/builder/AlipayTradePrecreateContentBuilder.php';
            require_once dirname ( __FILE__ ).'/alipay/f2fpay/service/AlipayTradeService.php';
            $timeExpress = "30m";
            $qrPayRequestBuilder = new \AlipayTradePrecreateContentBuilder();
            $qrPayRequestBuilder->setOutTradeNo($orderId);
            $qrPayRequestBuilder->setTotalAmount($rmb);
            $qrPayRequestBuilder->setTimeExpress($timeExpress);
            $qrPayRequestBuilder->setSubject($title);
            $qrPayRequestBuilder->setBody($body);
            $qrPay = new \AlipayTradeService($config);
            $qrPayResult = $qrPay->qrPay($qrPayRequestBuilder, $notify_url);
            $response = $qrPayResult->getResponse();
            //  根据状态值进行业务处理
            if ($qrPayResult->getTradeStatus() === 'SUCCESS') {
                $url = index_url("pay/qrcode/index", ['data' => $response->qr_code, 'pay_type' => 'alipay']);
                return ['status' => 'success', 'message' => '二维码生成成功', 'data' => '<img src="'.$url.'" witdh="174" height="174" />'];
            } else {
                return ['status' => 'error', 'message' => '二维码生成失败', 'data' => $response];
            }
        } else {
            return ['status' => 'error', 'message' => '系统暂时关闭支付宝支付'];
        }
    }

    /**
     * 电脑网站支付请求
     * @param  string $orderId    订单号
     * @param  string $title      订单标题
     * @param  float  $rmb        订单总金额
     * @param  string $notify_url 异步通知地址
     * @param  string $body       订单描述
     * @return string             
     */
    public static function pagePay($orderId, $title, $rmb, $notify_url, $body = "") 
    {
        $config = self::config();
        if (empty($config)) {
            return ['status' => 'error', 'message' => '未配置支付宝支付功能'];
        }
        if ($config['open'] == 1) {
        	require_once dirname ( __FILE__ ).'/alipay/pagepay/buildermodel/AlipayTradePagePayContentBuilder.php';
        	require_once dirname ( __FILE__ ).'/alipay/pagepay/service/AlipayTradeService.php';
        	$timeout_express   = "30m";
    		$payRequestBuilder = new \AlipayTradePagePayContentBuilder();
    		$payRequestBuilder->setBody($body);
    		$payRequestBuilder->setSubject($title);
    		$payRequestBuilder->setTotalAmount($rmb);
    		$payRequestBuilder->setOutTradeNo($orderId);
    		$payRequestBuilder->setTimeExpress($timeout_express);
    		$aop = new \AlipayTradeService($config);
    		$result = $aop->pagePay($payRequestBuilder,'',$notify_url);
    		return $result;
        } else {
            return ['status' => 'error', 'message' => '系统暂时关闭支付宝支付'];
        }
    }

    /**
     * 手机网站支付请求
     * @param  string $orderId    订单号
     * @param  string $title      订单标题
     * @param  float  $rmb        订单总金额
     * @param  string $notify_url 异步通知地址
     * @param  string $body       订单描述
     * @return string             
     */
    public static function wapPay($orderId, $title, $rmb, $notify_url, $body = "") 
    {
        $config = self::config();
        if (empty($config)) {
            return ['status' => 'error', 'message' => '未配置支付宝支付功能'];
        }
        if ($config['open'] == 1) {
        	require_once dirname ( __FILE__ ).'/alipay/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php';
        	require_once dirname ( __FILE__ ).'/alipay/wappay/service/AlipayTradeService.php';
    	    $timeout_express = "30m";
    	    $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
    	    $payRequestBuilder->setBody($body);
    	    $payRequestBuilder->setSubject($title);
    	    $payRequestBuilder->setOutTradeNo($orderId);
    	    $payRequestBuilder->setTotalAmount($rmb);
    	    $payRequestBuilder->setTimeExpress($timeout_express);
    	    $payResponse = new \AlipayTradeService($config);
    	    $result = $payResponse->wapPay($payRequestBuilder,'',$notify_url);
    	    return;
        } else {
            return ['status' => 'error', 'message' => '系统暂时关闭支付宝支付'];
        }
    }

    /**
     * 统一收单线下交易查询
     * @param  string $orderId    订单号
     * @return json   
     */
    public static function query($orderId)
    {
        $config = self::config();
        if (empty($config)) {
            return ['status' => 'error', 'message' => '未配置支付宝支付功能'];
        }
    	require_once dirname ( __FILE__ ).'/alipay/pagepay/buildermodel/AlipayTradeQueryContentBuilder.php';
    	require_once dirname ( __FILE__ ).'/alipay/pagepay/service/AlipayTradeService.php';
		$RequestBuilder = new \AlipayTradeQueryContentBuilder();
		$RequestBuilder->setOutTradeNo($orderId);
		$RequestBuilder->setTradeNo('');
		$aop = new \AlipayTradeService($config);
		$response = $aop->Query($RequestBuilder);
        if ($response->code == 10000) {
            if ($response->trade_status == 'TRADE_SUCCESS') {
                return ['status' => 'success', 'message' => '交易支付成功', 'data' => $response];
            } else {
                return ['status' => 'info', 'message' => '交易查询成功', 'data' => $response];
            }
        } else {
            return ['status' => 'error', 'message' => '交易查询失败', 'data' => $response];
        }
    }

    /**
     * 统一收单交易退款
     * @param  string $orderId    订单号
     * @param  float  $rmb        退款金额
     * @param  string $reason     退款说明
     * @return json   
     */
    public static function refund($orderId, $rmb, $reason = "")
    {
        $config = self::config();
        if (empty($config)) {
            return ['status' => 'error', 'message' => '未配置支付宝支付功能'];
        }
        if ($config['open'] == 1) {
    		require_once dirname ( __FILE__ ).'/alipay/pagepay/buildermodel/AlipayTradeRefundContentBuilder.php';
    		require_once dirname ( __FILE__ ).'/alipay/pagepay/service/AlipayTradeService.php';
    		$RequestBuilder = new \AlipayTradeRefundContentBuilder();
    		$RequestBuilder->setOutTradeNo($orderId);
    		$RequestBuilder->setTradeNo('');
    		$RequestBuilder->setRefundAmount($rmb);
    		$RequestBuilder->setOutRequestNo($orderId);
    		$RequestBuilder->setRefundReason($reason);
    		$aop = new \AlipayTradeService($config);
    		$response = $aop->Refund($RequestBuilder);
    		if ($response->code == 10000) {
                return ['status' => 'success', 'message' => '退款成功', 'data' => $response];
            } else {
                return ['status' => 'error', 'message' => '退款失败', 'data' => $response];
            }
        } else {
            return ['status' => 'error', 'message' => '系统暂时关闭支付宝支付'];
        }
    }

    /**
     * 统一收单线下交易退款查询
     * @param  string $orderId    订单号
     * @return json   
     */
    public static function refundQuery($orderId)
    {
        $config = self::config();
        if (empty($config)) {
            return ['status' => 'error', 'message' => '未配置支付宝支付功能'];
        }
        if ($config['open'] == 1) {
        	require_once dirname ( __FILE__ ).'/alipay/pagepay/buildermodel/AlipayTradeFastpayRefundQueryContentBuilder.php';
        	require_once dirname ( __FILE__ ).'/alipay/pagepay/service/AlipayTradeService.php';
    		$RequestBuilder = new \AlipayTradeFastpayRefundQueryContentBuilder();
    		$RequestBuilder->setOutTradeNo($orderId);
    		$RequestBuilder->setTradeNo('');
    		$RequestBuilder->setOutRequestNo($orderId);
    		$aop = new \AlipayTradeService($config);
    		$response = $aop->refundQuery($RequestBuilder);
    		if ($response->alipay_trade_fastpay_refund_query_response->code == 10000) {
                return ['status' => 'success', 'message' => '退款查询成功', 'data' => $response->alipay_trade_fastpay_refund_query_response];
            } else {
                return ['status' => 'error', 'message' => '退款查询失败', 'data' => $response->alipay_trade_fastpay_refund_query_response];
            }
        } else {
            return ['status' => 'error', 'message' => '系统暂时关闭支付宝支付'];
        }
    }
}